package com.neuedu.ssm.controller;

import com.neuedu.ssm.entity.Menu;
import com.neuedu.ssm.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 项目：      spring-framework
 * 类名:       MenuController
 * 创建时间：  2024/3/22 15：27
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    IMenuService menuService;



    @RequestMapping("/list")
    String list(Model model, Menu menu) {

        model.addAttribute("list", menuService.queryList(menu));
        return "/menu/menu_list";
    }

    //跳转到添加页面

    @RequestMapping("/toAdd")
    String toAdd() {  return "/menu/menu_add"; }

    /**
     * 跳转到修改页面
     * @param model
     * @param menuId
     * @return
     */
    @RequestMapping("/toEdit/{menuId}")
    String toEdit(Model model , @PathVariable  Integer menuId) {
        model.addAttribute("menu", menuService.queryOne(menuId));
        return "/menu/menu_edit";
    }


    @RequestMapping("saveOrUpdate")
    String saveOrUpdate(Menu menu) {

        boolean success = menuService.saveOrUpdate(menu);

        return "redirect:/menu/list"; }


    @ResponseBody
    @RequestMapping("delete/{id}")
    Map delete(@PathVariable int id) {
        Map result = new HashMap();
        boolean success = false;
        try{
            success = menuService.deleteById(id);
        }catch (Exception e){
            result.put("msg",e.getMessage());
        }

        result.put("success",success);

        return result;
    }

}
